#!/bin/sh
. /etc/init.d/functions
#若没expect则安装
[[ -f /usr/bin/expect ]] || { echo "install expect";yum install expect -y; } 
#安装失败则退出
[ $? = 0 ] || { echo "expect安装失败";exit; } 
#公钥路径
PUB=/`whoami`/.ssh/id_dsa.pub
#若没公钥则生成
[[ -f $PUB ]] || { ssh-keygen -t dsa -P "" -f /`whoami`/.ssh/id_dsa>/dev/null 2>&1; }

#登录用户和密码
USER=`whoami`
#PASS=123456

PASS=
if [ -z $PASS ]
then
  read -p "set remote host password:" PASS
  if [ -z $PASS ]
  then
    PASS=123456
    echo "use $PASS as remote host password."
  fi
fi

#expect自动交互
function EXP() {
/usr/bin/expect << EOF
set timeout 5
spawn /usr/bin/ssh-copy-id -i $PUB  $USER@$SIP$n
expect {
        "*yes/no*" { send "yes\r";exp_continue }
        "password:" { send "$PASS\n";exp_continue } 
        eof { exit }
        }
EOF
}
#
for n in $*
do
  EXP >/dev/null 2>&1
  echo $n hostname >/dev/null 2>&1
  [[ $? == 0 ]] && action "========$n" /bin/true || action "========$n" /bin/false
done
